package com.kk.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kk.score.dao.TaskDao;
import com.kk.score.entity.DepartmentEntity;
import com.kk.score.entity.TaskEntity;
import com.kk.score.entity.vo.TaskPersonVo;
import com.kk.score.entity.vo.UnitVo;
import com.kk.score.utils.DateUtils;
import com.kk.score.utils.PageUtils;
import com.kk.score.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.kk.score.dao.PushDao;
import com.kk.score.entity.PushEntity;
import com.kk.score.service.PushService;
import org.springframework.util.StringUtils;


@Service("pushService")
public class PushServiceImpl extends ServiceImpl<PushDao, PushEntity> implements PushService {

    @Autowired
    private PushDao pushDao;

    @Autowired
    private TaskDao taskDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String status = (String) params.get("status");
        String year = (String) params.get("year");
        if (!StringUtils.hasText(year)) {
            year = DateUtils.getYears();
        }
        if (!StringUtils.hasText(status)) {
            status = "1";
        }
        IPage<?> pushPage = pushDao.findPushPage(new Query<UnitVo>().getPage(params), status, year);
        @SuppressWarnings("unchecked")
        List<UnitVo> records = (List<UnitVo>) pushPage.getRecords();
        for (UnitVo record : records) {
            String uid = record.getPushUid();
            // 查询item的任务集合
            List<TaskPersonVo> taskEntities = taskDao.findTasksByPushUid(uid);
            //查询item的任务集合
//            List<TaskEntity> taskEntities = taskDao.selectList(new LambdaQueryWrapper<TaskEntity>().eq(TaskEntity::getPushUid, uid));
            boolean isSuccess = true;
            double averageScore = 0.0;
            double averageScoreByLead = 0.0;
            //未完成人数
            int person = 0;
            // 校领导人数
            int personByLead = 0;
            for (TaskPersonVo taskEntity : taskEntities) {
                if (taskEntity.getStatus() == 1
                        && taskEntity.getDepartmentName().equals("校领导")
                        && taskEntity.getDepartmentUid().equals("c3a76631ca644951c62444051e5f5290")
                ) {
                    personByLead++;
                }
            }
            // 教学单位人数
            int count = taskEntities.size() - personByLead;
            for (TaskPersonVo taskEntity : taskEntities) {
                record.setStartTime(taskEntity.getStartTime());
                record.setEndTime(taskEntity.getEndTime());
                if (!taskEntity.getDepartmentName().equals("测试单位")
                        && !taskEntity.getDepartmentUid().equals("1541f07cee52da96d84878a322716103")
                ) {
                    if (taskEntity.getStatus() == 1
                            && !taskEntity.getDepartmentName().equals("校领导")
                            && !taskEntity.getDepartmentUid().equals("c3a76631ca644951c62444051e5f5290")
                    ) {
                        averageScore = averageScore + taskEntity.getScore() / count;
                    } else if (taskEntity.getStatus() == 1
                            && taskEntity.getDepartmentName().equals("校领导")
                            && taskEntity.getDepartmentUid().equals("c3a76631ca644951c62444051e5f5290")
                    ) {
                        averageScoreByLead = averageScoreByLead + taskEntity.getScore() / personByLead;
                    }
                    //若任务中有一个任务的状态不为已完成，则设置标志为false
                    if (taskEntity.getStatus() != 1) {
                        isSuccess = false;
                        person += 1;
                    }
                }
            }
            //设置是否完成评价
            record.setIsSuccess(isSuccess ? "已完成" : "未完成");
            //设置平均分 若没有评价完，值为0
            DecimalFormat df = new DecimalFormat("#.000");
            record.setScore(Double.parseDouble(df.format(averageScore)));
            record.setScoreByLead(Double.parseDouble(df.format(averageScoreByLead)));
            //设置未完成人数
            record.setPerson(person);
        }
        return new PageUtils(pushPage);
    }

}